package com.ruoyi.base.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BusWarnWarninfoMapper;
import com.ruoyi.base.domain.BusWarnWarninfo;
import com.ruoyi.base.service.IBusWarnWarninfoService;
import org.springframework.util.CollectionUtils;

/**
 * 预警信息数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-16
 */
@Service
public class BusWarnWarninfoServiceImpl implements IBusWarnWarninfoService 
{
    @Autowired
    private BusWarnWarninfoMapper busWarnWarninfoMapper;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询预警信息数据
     * 
     * @param id 预警信息数据主键
     * @return 预警信息数据
     */
    @Override
    public BusWarnWarninfo selectBusWarnWarninfoById(String id)
    {
        return busWarnWarninfoMapper.selectBusWarnWarninfoById(id);
    }

    /**
     * 查询预警信息数据列表
     * 
     * @param busWarnWarninfo 预警信息数据
     * @return 预警信息数据
     */
    @Override
    public List<BusWarnWarninfo> selectBusWarnWarninfoList(BusWarnWarninfo busWarnWarninfo)
    {
        if(StringUtils.isNotEmpty(busWarnWarninfo.getBeginTime())){
            busWarnWarninfo.setBeginTime(busWarnWarninfo.getBeginTime()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(busWarnWarninfo.getEndTime())){
            busWarnWarninfo.setEndTime(busWarnWarninfo.getEndTime()+" 23:59:59");
        }
        return busWarnWarninfoMapper.selectBusWarnWarninfoList(busWarnWarninfo);
    }

    /**
     * 新增预警信息数据
     * 
     * @param busWarnWarninfo 预警信息数据
     * @return 结果
     */
    @Override
    public int insertBusWarnWarninfo(BusWarnWarninfo busWarnWarninfo)
    {
        busWarnWarninfo.setCreateTime(DateUtils.getNowDate());
        return busWarnWarninfoMapper.insertBusWarnWarninfo(busWarnWarninfo);
    }

    /**
     * 修改预警信息数据
     * 
     * @param busWarnWarninfo 预警信息数据
     * @return 结果
     */
    @Override
    public int updateBusWarnWarninfo(BusWarnWarninfo busWarnWarninfo)
    {
        busWarnWarninfo.setUpdateTime(DateUtils.getNowDate());
        return busWarnWarninfoMapper.updateBusWarnWarninfo(busWarnWarninfo);
    }

    /**
     * 批量删除预警信息数据
     * 
     * @param ids 需要删除的预警信息数据主键
     * @return 结果
     */
    @Override
    public int deleteBusWarnWarninfoByIds(String[] ids)
    {
        return busWarnWarninfoMapper.deleteBusWarnWarninfoByIds(ids);
    }

    @Override
    public int closeBusWarnWarninfoByIds(String[] ids) {
        int count = ids.length;
        for (String id : ids) {
            BusWarnWarninfo busWarnWarninfo = selectBusWarnWarninfoById(id);
            if(busWarnWarninfo != null){
                busWarnWarninfo.setUserid(SecurityUtils.getUserId().toString());
                busWarnWarninfo.setProcessingstate("3");
                busWarnWarninfo.setUpdateTime(DateUtils.getNowDate());
                updateBusWarnWarninfo(busWarnWarninfo);
            }
        }
        return count;
    }

    @Override
    public int auditBusWarnWarninfoById(String id,String status,String remark) {
        BusWarnWarninfo busWarnWarninfo = selectBusWarnWarninfoById(id);
        if(busWarnWarninfo != null){
            busWarnWarninfo.setUserid(SecurityUtils.getUserId().toString());
            busWarnWarninfo.setProcessingstate(status);
            busWarnWarninfo.setUpdateTime(DateUtils.getNowDate());
            busWarnWarninfo.setControlSuggestions(remark);
            updateBusWarnWarninfo(busWarnWarninfo);
        }
        return 1;
    }

    /**
     * 删除预警信息数据信息
     * 
     * @param id 预警信息数据主键
     * @return 结果
     */
    @Override
    public int deleteBusWarnWarninfoById(String id)
    {
        return busWarnWarninfoMapper.deleteBusWarnWarninfoById(id);
    }

    @Override
    public List<Map> statisTypeCount(String startDay, String endDay) {
        startDay = startDay+" 00:00:00";
        endDay = endDay + " 23:59:59";
        return busWarnWarninfoMapper.statisTypeCount(startDay,endDay);
    }

    @Override
    public Map statisWarnLevelCount(String startDay, String endDay) {
        Map resMap = new HashMap();

        List<String> typeList = new ArrayList<>();
        List<String> typeNameList = new ArrayList<>();
        Map typeToNameMap = new HashMap();

        SysDictData sysDictDataParam = new SysDictData();
        sysDictDataParam.setDictType("warn_level");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(sysDictDataParam);

        for (SysDictData sysDictData : dictDataList) {
            typeNameList.add(sysDictData.getDictLabel());
            typeList.add(sysDictData.getDictValue());
            typeToNameMap.put(sysDictData.getDictValue(),sysDictData.getDictLabel());
        }
        startDay = startDay+" 00:00:00";
        endDay = endDay + " 23:59:59";
        List<Map> dataList = busWarnWarninfoMapper.statisWarnLevelCount(startDay,endDay);
        List<Map> propoList = new ArrayList<>();
        int allCount = 0;
        List<Map> levelDataList = new ArrayList<>();
        for(int i=0;i<typeList.size();i++){
            String levelType = typeList.get(i);
            Map levelDataMap = new HashMap();
            Integer count = 0;
            if(!CollectionUtils.isEmpty(dataList)){
                for (Map map : dataList) {
                    String warnLevel = map.get("warnLevel").toString();
                    if(levelType.equals(warnLevel)){
                        count = Integer.parseInt(map.get("dataCount").toString());
                        allCount = allCount + count;
                        break;
                    }
                }
            }
            levelDataMap.put("warnLevel",levelType);
            String warnLevelName = "";
            if(typeToNameMap.containsKey(levelType)){
                warnLevelName = typeToNameMap.get(levelType).toString();
            }else{
                warnLevelName = "未知";
            }

            levelDataMap.put("dataCount",count);
            levelDataMap.put("warnLevelName",warnLevelName);
            levelDataList.add(levelDataMap);

            Map propoMap = new HashMap();
            propoMap.put("warnLevel",levelType);
            propoMap.put("warnLevelName",warnLevelName);
            propoMap.put("dataCount",count);
            propoList.add(propoMap);
        }


        for (Map map : propoList) {
            Integer dataCount = Integer.parseInt(map.get("dataCount").toString());
            if(allCount == 0){
                map.put("propor","0%");
            }else{
                Float value = ((float) dataCount/(float)allCount)*100f;
                map.put("propor",String.format("%.2f",value)+"%");
            }
        }
        resMap.put("dataList",levelDataList);
        resMap.put("proporList",propoList);
        return resMap;
    }

    @Override
    public Map statisStatusCount(String startDay, String endDay) {
        List<String> typeList = new ArrayList<>();
        List<String> typeNameList = new ArrayList<>();
        SysDictData sysDictDataParam = new SysDictData();
        sysDictDataParam.setDictType("warn_status");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(sysDictDataParam);

        for (SysDictData sysDictData : dictDataList) {
            typeNameList.add(sysDictData.getDictLabel());
            typeList.add(sysDictData.getDictValue());
        }

        Map resMap = new HashMap();
        startDay = startDay+" 00:00:00";
        endDay = endDay + " 23:59:59";
        List<Integer> statusCountList = new ArrayList<>();

        List<Map> dataList = busWarnWarninfoMapper.statisStatusCount(startDay,endDay);
        for(int i=0;i<typeList.size();i++){
            String type = typeList.get(i);
            int count = 0;
            if(!CollectionUtils.isEmpty(dataList)){
                for (Map map : dataList) {
                    String status = map.get("processingState").toString();
                    if(type.equals(status)){
                        count = Integer.parseInt(map.get("dataCount").toString());
                        break;
                    }
                }
            }
            statusCountList.add(count);
        }
        resMap.put("statusTypeList",typeNameList);
        resMap.put("statusCountList",statusCountList);
        return resMap;
    }

    @Override
    public Map statisTypeAndTimeCount(String startDay, String endDay) {
        List<String> dayList = DateUtils.getdayBetweenDate(startDay,endDay);
        List<String> typeList = new ArrayList<>();
        List<String> typeNameList = new ArrayList<>();

        int allCount = 0;


        SysDictData sysDictDataParam = new SysDictData();
        sysDictDataParam.setDictType("warn_type_data");
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(sysDictDataParam);

        for (SysDictData sysDictData : dictDataList) {
            typeNameList.add(sysDictData.getDictLabel());
            typeList.add(sysDictData.getDictValue());
        }

        Map resMap = new HashMap();
        startDay = startDay+" 00:00:00";
        endDay = endDay + " 23:59:59";
        List<Map> statisList = busWarnWarninfoMapper.statisTypeAndTimeCount(startDay,endDay);
        List<List<Integer>> dataCountList = new ArrayList<>();
        List<Map> proporList = new ArrayList<>();
        for(int i=0;i<typeList.size();i++){
            String type = typeList.get(i);
            String typeName = typeNameList.get(i);
            List<Integer> valueList = new ArrayList<>();
            int typeCount = 0;
            for (String day : dayList) {
                int count = 0;
                if(!CollectionUtils.isEmpty(statisList)){
                    for (Map map : statisList) {
                        String dataDay = map.get("dataDay").toString();
                        String warnType = map.get("warnType").toString();
                        int dataCount = Integer.parseInt(map.get("dataCount").toString());
                        if(type.equals(warnType) && day.equals(dataDay)){
                            count = dataCount;
                            allCount = allCount + count;
                            typeCount = typeCount + count;
                            break;
                        }
                    }
                }
                valueList.add(count);
            }
            Map proporMap = new HashMap();
            proporMap.put("typeName",typeName);
            proporMap.put("typeCount",typeCount);
            proporList.add(proporMap);
            dataCountList.add(valueList);
        }
        for (Map map : proporList) {
            Integer typeCount = Integer.parseInt(map.get("typeCount").toString());
            if(allCount == 0){
                map.put("propor","0%");
            }else{
                Float value = ((float) typeCount/(float)allCount)*100f;
                map.put("propor",String.format("%.2f",value)+"%");
            }
        }
        resMap.put("typeList",typeNameList);
        resMap.put("dayList",dayList);
        resMap.put("dataCountList",dataCountList);
        resMap.put("proporList",proporList);
        return resMap;
    }
}
